﻿/*
RELEASE 時清除數據及子物件.
*/
package com.cf.core.model{
	import flash.display.MovieClip;
	import flash.events.Event;
	import com.cf.core.Utils.*;
	import flash.display.DisplayObjectContainer;

	public class AutoDestoryClip extends MovieClip{
		private var G_el:Array;
		public function init():void{}
		public function inited():void{}
		public function AutoDestoryClip():void{
			G_el=new Array();
			this.addEventListener(Event.ADDED_TO_STAGE, handleOnload);
			this.addEventListener(Event.REMOVED, RELEASE);
			init();
		}//fn
		private function handleOnload(e:Event=null):void{
			this.removeEventListener(Event.ADDED_TO_STAGE, handleOnload);
			inited();
		}//fn
		// function auto release listener
		override public function addEventListener(_type:String, _listener:Function, _useCapture:Boolean=false, _priority:int=0, _useWeakReference:Boolean=false):void{
			try{
				super.addEventListener(_type,_listener,_useCapture,_priority,_useWeakReference);
				G_el.push({
					type:_type,
					listener:_listener,
					useCapture:_useCapture
				});
			}catch(e){}
		}//fn
		override public function removeEventListener(_type:String, _listener:Function, _useCapture:Boolean=false):void{
			super.removeEventListener(_type, _listener, _useCapture);
		}//fn
		public function RELEASE(e:Event=null):void{
			if(e!=null) if(e.currentTarget != e.target) return;
			stop();
			tools.emptySprite(this);
			for(var k:String in this){ delete this[k]; }
			for(var i:uint=0;i<G_el.length;i++){
				try{
					removeEventListener(G_el[i].type,G_el[i].listener,G_el[i].useCapture);
				}catch(ee){ trace("AutoDestoryClip : "+ this.name +" eventlister not found."); }
			}//for
			G_el = null;
			trace("AutoDestoryClip : "+ this.name +" RELEASE finished");
		}//fn
	}//class
}//package